package com.dingwen.treasure.mybatisplus.listener;

import com.dingwen.treasure.base.constant.RedisConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import javax.annotation.Resource;

/**
 * redis 监听配置 </br>
 * <p> 待优化点：ConfigCacheListener、RuleCacheListener 强制性依赖，待解耦 </p>
 * @author dingwen
 * @desc
 * @date 2022/05/12 11:42
 */
@Configuration
public class RedisListenerConfig {

    @Resource
    private ConfigCacheListener configCacheListener;

    @Resource
    private RuleCacheListener ruleCacheListener;

    /**
     * 消息监听容器
     * @param factory
     * @return
     */
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory factory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(factory);
        //订阅一个通道 该处的通道名是发布消息时的名称
        container.addMessageListener(configCacheListener, new ChannelTopic(RedisConstant.REDIS_REFRESH_CACHE_CHANNEL_CONFIG));
        container.addMessageListener(ruleCacheListener, new ChannelTopic(RedisConstant.REDIS_REFRESH_CACHE_CHANNEL_BUSINESS_RULE));
        return container;
    }

}
